package servlets;

import java.io.IOException;
import java.util.List;
import beans.AdminBean;
import beans.EmployeeBean;
import beans.CarRequestBean;
import beans.EmployeeCarInfoBean;
import beans.Driver_TaskBean;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import service.UserService;

public class C_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		String RequestID =request.getParameter("RequestID");
		String ID =request.getParameter("ID");
		//System.out.println("RD"+RequestID+ID);
		System.out.println("action:"+action);
		
		if("login".equals(action))
		{
			login(request, response);
		}
		if("addStaff_enroll".equals(action))
		{
			addStaff_enroll(request, response);
		}
		if("staff_UseCar_submit".equals(action))
		{
			staff_UseCar_submit(request, response);
		}
		if("C_Check_Search".equals(action))
		{
			C_Check_Search(request, response);
		}
		if("C_CheckSingle_Search".equals(action))
		{
			C_CheckSingle_Search(request, response);
		}
		if("C_DriverTask_Search".equals(action))
		{
			C_DriverTask_Search(request, response);
		}
		if("C_AdminSubmit".equals(action))
		{
			C_AdminSubmit(request, response);
		}
		if("C_AdminReturn".equals(action))
		{
			C_AdminReturn(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	public void C_AdminReturn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		UserService userService=new UserService();
		//��ȡ������ת��Ϊ��Ӧ������
		String RequestID=request.getParameter("RequestID");
		//�����û����󣬲����뵽���ݱ�users��
		CarRequestBean user=new CarRequestBean();
		user.setRequestID(RequestID);
		int delResult=userService.AdminReturn(user);
		String path="/C-fail.jsp";
	    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
		RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
		requestDistpatcher.forward(request, response);				

	}
	
	public void C_AdminSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		UserService userService=new UserService();
		String RequestID=request.getParameter("RequestID");
		String Status=null;
		String AgreeOrNot=request.getParameter("AgreeOrNot");
		String CensorContent=request.getParameter("CensorContent");
		String Adminname=request.getParameter("Adminname");
		String AdminTime=request.getParameter("AdminTime");
		System.out.println(" RequestID:"+ RequestID+"AgreeOrNot:"+AgreeOrNot);
		if(AgreeOrNot.equals("同意"))
			Status="审核通过";
		else
			Status="审核未通过";
	
		CarRequestBean user=new CarRequestBean();
		user.setRequestID(RequestID);
		user.setStatus(Status);
		user.setAgreeOrNot(AgreeOrNot);
		user.setCensorContent(CensorContent);
		user.setAdminname(Adminname);
		user.setAdminTime(AdminTime);
		int edit=userService.AdminSubmit_edit(user);
		if(edit==1)
		{
			request.setAttribute("addok","1");			
		}
		else
		{
			request.setAttribute("addok","-1");
		}
	
		String path="/C-Use_Check.jsp";
		response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
		RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
		requestDistpatcher.forward(request, response);		
	}
	
	public void C_DriverTask_Search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		UserService userService=new UserService();
		List <Driver_TaskBean> list=userService.Driver_Task_browse();
		request.setAttribute("allUsers", list);
		request.setAttribute("first", "1");
		String path="/X-Driver_Task.jsp";
	    response.setContentType("text/html;charset=UTF-8");
		RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
		requestDistpatcher.forward(request, response);

	}
	
	public void C_Check_Search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String tt=request.getParameter("tt");
		String EpID=request.getParameter("EpID");
		String time=request.getParameter("time");
		//System.out.println("servlet: EpID:"+EpID);
		UserService userService=new UserService();
		List <CarRequestBean> list=userService.CarRequestBean_browse();
		request.setAttribute("allUsers", list);
		request.setAttribute("first", "1");
		request.setAttribute("EpID", EpID);
		request.setAttribute("time", time);
		String path=null;
		if(tt.equals("1"))
			path="/C-Use_Check.jsp";
		else if(tt.equals("2"))
			path="/C-success.jsp";
		else if(tt.equals("3"))
			path="/C-fail.jsp";
		else
			path="/D-car_manage.jsp";
	    response.setContentType("text/html;charset=UTF-8");
		RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
		requestDistpatcher.forward(request, response);

	}
	
	public void C_CheckSingle_Search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		
		String RequestID =request.getParameter("RequestID");
		String ID =request.getParameter("ID");
		System.out.println("RD"+RequestID+ID);
		UserService userService=new UserService();
		CarRequestBean list=userService.getCarRequest_getRecord(RequestID);
		AdminBean Admin=userService.getAdmin_getRecord(ID);
		request.setAttribute("User", list);
		request.setAttribute("Admin", Admin);
		request.setAttribute("frist", "1");
		String path="/C-UseCar_CheckList.jsp";
	    response.setContentType("text/html;charset=UTF-8");
		RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
		requestDistpatcher.forward(request, response);

	}
	
	
	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String ID=request.getParameter("ID");
		String password=request.getParameter("password");
		String department=request.getParameter("department");
		
		UserService userService=new UserService();
		if(userService.login(ID, password, department))
		{
			AdminBean user =new AdminBean();
			//user.setAccount(account);
			HttpSession session = request.getSession(); 
			session.setAttribute("user", user);
			/*List <AdminBean> list=userService.browse();
			request.setAttribute("allUsers", list);*/
			if ("管理端".equals(department))
			{		
				session.setAttribute("ID", ID);
				request.setAttribute("ID", ID);                                           
				response.getWriter().println("<script>window.parent.location.href='C-Admin.jsp';</script>");
			}else if("财务部".equals(department))
			{		
				session.setAttribute("ID", ID);
				response.getWriter().println("<script>window.parent.location.href='G-Finance.jsp';</script>");
			 
			}else if("运输端".equals(department))
			{
				session.setAttribute("ID", ID);
				response.getWriter().println("<script>window.parent.location.href='D-car_dispatch.jsp';</script>");
			}else if("司机".equals(department))
			{
				session.setAttribute("Did", ID);
				response.getWriter().println("<script>window.parent.location.href='X-Driver_login.jsp';</script>");
			}
			else 
			{
				session.setAttribute("EmpID", ID);			
				response.getWriter().println("<script>window.parent.location.href='X-UseCar_login.jsp';</script>");
			}
			
		   /* response.setContentType("text/html;charset=UTF-8");
			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
			requestDistpatcher.forward(request, response);*/
			
		}
		else
		{
			request.setAttribute("loginok","0");
			request.setAttribute("lastID",ID);
			request.setAttribute("lastpassword",password);
			String path="C-form_login.jsp";
		    response.setContentType("text/html;charset=UTF-8");
			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
			requestDistpatcher.forward(request, response);
		}		
	}
	
	public void addStaff_enroll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		UserService userService=new UserService();
		String EmpID=request.getParameter("EmpID");
		String password=request.getParameter("password");
		String phone=request.getParameter("phone");
		
		if(userService.enroll_valid(EmpID))
		{
			EmployeeBean user=new EmployeeBean();
			user.setEmpID(EmpID);
			user.setpassword(password);
			user.setPhone(phone);
			int edit=userService.enroll_edit(user);
			if(edit==1)
			{
				request.setAttribute("loginok","1");			
			}
			else
			{
				request.setAttribute("loginok","-1");
			}
		}
		else
			request.setAttribute("loginok","0");
		String path="/C-form_enroll.jsp";
		response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
		RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
		requestDistpatcher.forward(request, response);		
	}
	
	public void staff_UseCar_submit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		UserService userService=new UserService();
		//��ȡ������ת��Ϊ��Ӧ������
		String RequestID=userService.getLastRequestID();
		String UsersNum=request.getParameter("UsersNum");
		int UsersNumInt=Integer.parseInt(UsersNum);
		String UseRange=request.getParameter("UseRange");
		String Userphone=request.getParameter("Userphone");
		String UseTime=request.getParameter("UseTime");
		String ReturnTime=request.getParameter("ReturnTime");
		String degree=request.getParameter("degree");
		String EmpID=request.getParameter("EmpID");
		String applyTime=request.getParameter("applyTime");
		String applyDepartment=request.getParameter("applyDepartment");
		String origin=request.getParameter("origin");
		String goPlace=request.getParameter("goPlace");
		String UseReasons=request.getParameter("UseReasons");
		String Remarks=request.getParameter("Remarks");
		String Attachment =request.getParameter("Attachment");
		String Status="待审核";
		String Did="待指派";
		String license_plate="待指派";
		String Dname="尚未确定";
		String Dphone="尚未确定";
	
		//EmployeeCarInfoBean
		CarRequestBean user1=new CarRequestBean();
		user1.setRequestID(RequestID);
		user1.setUsersNum(UsersNumInt);
		user1.setUseRange(UseRange);
		user1.setUseTime(UseTime);
		user1.setReturnTime(ReturnTime);
		user1.setUserphone(Userphone);
		user1.setdegree(degree);
		user1.setEmpID(EmpID);
		user1.setapplyTime(applyTime);
		user1.setapplyDepartment(applyDepartment);
		user1.setorigin(origin);
		user1.setgoPlace(goPlace);
		user1.setUseReasons(UseReasons);
		user1.setRemarks(Remarks);
		user1.setAttachment(Attachment);
		user1.setStatus(Status);
		user1.setDid(Did);
		user1.setlicense_plate(license_plate);
		
		EmployeeCarInfoBean user2=new EmployeeCarInfoBean();
		user2.setRequestID(RequestID);
		user2.setEmpID(EmpID);
		user2.setUseTime(UseTime);
		user2.setReturnTime(ReturnTime);
		user2.setDid(Did);
		user2.setDname(Dname);
		user2.setDphone(Dphone);
		user2.setlicense_plate(license_plate);
		user2.setUsersNum(UsersNumInt);
		user2.setorigin(origin);
		user2.setgoPlace(goPlace);
		
		if(userService.staff_addCarRequest(user1)&&userService.staff_addInfo(user2))
		{
			request.setAttribute("addok","1");								
		}
		String path="/X-UseCar_CheckList.jsp";
	    response.setContentType("text/html;charset=UTF-8");
		RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
		requestDistpatcher.forward(request, response);
	}
}
